package com.group.model;

import java.util.List;
import java.util.Random;

import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Async;

import com.alibaba.fastjson.annotation.JSONField;

/**
 * 头脑游戏
 * 
 * @author zhaolei
 *
 */
public class BrainGame implements Runnable {

	private Long id;

	/**
	 * 用户1
	 */
	private User user1;

	/**
	 * 用户2(暂时机器人)
	 */
	private BrainRobot user2;

	/**
	 * 状态,0(未开始),1,2,3,4,5,100(结束) (1,2,3,4,5 代表第几题)
	 */
	private Integer state;

	private Integer sec;

	/**
	 * 进行状态
	 */
	private boolean flag = true;

	public void setSec(Integer sec) {
		this.sec = sec;
	}

	/**
	 * 5道题
	 */
	private List<BrainQuestion> questions;

	/**
	 * 当前题目
	 */
	private BrainQuestion currentQuestion;

	/**
	 * 第一用户得分
	 */
	private Integer score1;

	/**
	 * 第二用户得分
	 */
	private Integer score2;

	/**
	 * 第一用户回答
	 */
	private String answer1;

	/**
	 * 第二用户回答
	 */
	private String answer2;

	/**
	 * 第一用户答题进度 从1开始
	 */
	private Integer progress1;

	/**
	 * 第二用户答题进度
	 */
	private Integer progress2;

	/**
	 * 当前时间
	 */
	private Long time;

	private Random r = new Random();;

	/**
	 * 开始游戏
	 * 
	 * @throws InterruptedException
	 */
	private void start() throws InterruptedException {
		// 和前端一样同步一个延时
		Thread.sleep(2000);

		while (flag) {

			if (state == 0) {
				// 未开始状态
				if (r.nextInt(100) > 70) {
					state = 1;
					currentQuestion = questions.get(0);
					time = System.currentTimeMillis();
					progress1 = 1;
					progress2 = 1;
					score1 = 0;
					score2 = 0;
					// 设置机器人答题时间
					if (user2 instanceof BrainRobot)
						user2.randomTime();
				}

				continue;
			} else if (state == 1) {
				// 答第一题
				answerQuestion();

			} else if (state == 2) {
				answerQuestion();
			} else if (state == 3) {
				answerQuestion();

			} else if (state == 4) {
				answerQuestion();

			} else if (state == 5) {
				answerQuestion();

			} else if (state == 100) {
				// 结束

				break;
			}

			Thread.sleep(200);
		}

	}

	/**
	 * 获得经过的时间
	 * 
	 * @return
	 */
	public Integer getSec() {
		try {
			sec = (int) ((System.currentTimeMillis() - time) / 1000);
		} catch (Exception e) {
			sec = 0;
		}
		return sec;
	}

	/**
	 * 获得分数
	 * 
	 * @param question
	 * @param answer
	 * @param sec
	 * @return
	 */
	private Integer getScore(BrainQuestion question, String answer, Integer sec) {
		if (question.getRight().equals(answer)) {
			// 回答正确
			int score = 200;

			/**
			 * 每过1秒减少20分
			 */
			score = score - (sec - 1) * 20;
			if (score < 0)
				score = 20;
			return score;

		} else {
			return 0;
		}

	}

	/**
	 * 判断是否超时,并且帮机器人答题
	 * 
	 * @param sec
	 */
	private void overTime(int sec) {
		if (sec >= 10) {
			// 超时,默认选错
			if (answer1 == null) {
				setAnswer1("超时未答题");
			}
			if (answer2 == null) {
				setAnswer2("超时未答题");
			}
		}

		// 帮助机器人答题
		if (user2 instanceof BrainRobot) {
			if (StringUtils.isEmpty(answer2)) {
				if (sec == user2.getTime()) {
					setAnswer2(user2.answer(currentQuestion));
				}
			}
		}
	}

	public void answerQuestion() {
		sec = getSec();
		// 判断是否超时,并且帮助机器人答题
		overTime(sec);

		if (StringUtils.isNotEmpty(answer1) && progress1 == state) {
			// 处理第一个用户答题
			score1 += getScore(currentQuestion, answer1, sec);

			progress1 = state + 1;
		}
		if (StringUtils.isNotEmpty(answer2) && progress2 == state) {
			// 处理第二个用户答题
			score2 += getScore(currentQuestion, answer2, sec);

			progress2 = state + 1;
		}

		if (progress1 == (state + 1) && progress2 == (state + 1)) {
			// 双方答题完毕,进入下一题
			// 加个小延时
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			answer1 = null;
			answer2 = null;
			time = System.currentTimeMillis();
			if (state == 5) {
				state = 100;
				return;
			}
			currentQuestion = questions.get(state);
			state = state + 1;

			// 重新设置机器人答题时间
			if (user2 instanceof BrainRobot)
				user2.randomTime();
		}
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public User getUser1() {
		return user1;
	}

	public void setUser1(User user1) {
		this.user1 = user1;
	}

	public BrainRobot getUser2() {
		return user2;
	}

	public void setUser2(BrainRobot user2) {
		this.user2 = user2;
	}

	public Integer getState() {
		return state;
	}

	public void setState(Integer state) {
		this.state = state;
	}

	public List<BrainQuestion> getQuestions() {
		return questions;
	}

	public void setQuestions(List<BrainQuestion> questions) {
		this.questions = questions;
	}

	public BrainQuestion getCurrentQuestion() {
		return currentQuestion;
	}

	public void setCurrentQuestion(BrainQuestion currentQuestion) {
		this.currentQuestion = currentQuestion;
	}

	public Integer getScore1() {
		return score1;
	}

	public void setScore1(Integer score1) {
		this.score1 = score1;
	}

	public Integer getScore2() {
		return score2;
	}

	public void setScore2(Integer score2) {
		this.score2 = score2;
	}

	public String getAnswer1() {
		return answer1;
	}

	public void setAnswer1(String answer1) {
		this.answer1 = answer1;
		this.currentQuestion.setAnswer1(answer1);
	}

	public String getAnswer2() {
		return answer2;
	}

	public void setAnswer2(String answer2) {
		this.answer2 = answer2;
		this.currentQuestion.setAnswer2(answer2);
	}

	public Integer getProgress1() {
		return progress1;
	}

	public void setProgress1(Integer progress1) {
		this.progress1 = progress1;
	}

	public Integer getProgress2() {
		return progress2;
	}

	public void setProgress2(Integer progress2) {
		this.progress2 = progress2;
	}

	public Long getTime() {
		return time;
	}

	public void setTime(Long time) {
		this.time = time;
	}

	@Override
	public void run() {
		try {
			start();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

}
